package com.kyrioscraft.cloud.security.core.social.qq.api;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.social.oauth2.AbstractOAuth2ApiBinding;
import org.springframework.social.oauth2.TokenStrategy;

/**
 * @Author kyrioscraft
 * @Date 2018/12/28 10:46
 * @Description //TODO
 **/
@Slf4j
public class QQImpl extends AbstractOAuth2ApiBinding implements QQ {
    private static final String URL_GET_OPENID = "https://graph.qq.com/oauth2.0/me?access_token=%s";
    private static final String URL_GET_USERINFO ="https://graph.qq.com/user/get_user_info? " +
            "oauth_consumer_key=%s&" +
            "openid=%s";
    private String appId;
    private String openId;
    public QQImpl(String accessToken, String appId) {
        super(accessToken, TokenStrategy.ACCESS_TOKEN_PARAMETER);
        this.appId = appId;

        String url = String.format(URL_GET_OPENID,accessToken);
        String result = getRestTemplate().getForObject(url,String.class);
        this.openId = StringUtils.substringBetween(result,"openid\":\"","\"}");
        log.info(result);
        log.info(this.openId);
    }
    @Override
    public QQUserInfo getUserInfo() {
        String url = String.format(URL_GET_USERINFO, appId, openId);
        QQUserInfo qqUserInfo = getRestTemplate().getForObject(url,QQUserInfo.class);
        log.info(qqUserInfo.getRet());
        return qqUserInfo;
    }
}
